﻿Imports System.IO

Public Class frmAttachFile

    Dim _PhotoPath As String = ""
    Public _ContractID As Long
    Public _CaseID As Long
    Public _isContract As Boolean = False
    Public _isFilePlaintiff As Boolean = False
    Public _isFileDefendant As Boolean = False
    Public _isFileThirdParty As Boolean = False

    Private Sub frmAttachFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reset()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _PhotoPath = OpenFileDialog.FileName
            txtFile.Text = _PhotoPath
        End If
    End Sub

    Protected Function getPhoto() As Byte()
        Dim pic As Byte() = Nothing
        If _PhotoPath <> "" Then
            pic = File.ReadAllBytes(_PhotoPath)
        End If
        Return pic
    End Function

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Protected Sub reset()
        txtFile.Clear()
        btnBrowse.Focus()
        _PhotoPath = ""
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtFile.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub isWhere(ByVal isContract As Boolean, ByVal isFilePlaintiff As Boolean, ByVal isFileDefendant As Boolean, ByVal isFileThirdParty As Boolean)
        _isContract = isContract
        _isFilePlaintiff = isFilePlaintiff
        _isFileDefendant = isFileDefendant
        _isFileThirdParty = isFileThirdParty
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If _isContract Then
            insert_File_Contract()
        ElseIf _isFilePlaintiff Then
            insert_File_CasePlaintiff()
        ElseIf _isFileDefendant Then
            insert_File_CaseDefendant()
        ElseIf _isFileThirdParty Then
            insert_File_CaseThirdParty()
        End If
    End Sub

    Protected Sub insert_File_Contract()
        Dim oFile As New cFile
        Dim FileName As String = System.IO.Path.GetFileName(_PhotoPath)
        If txtFile.Text <> "" Then
            If oFile.insert_File_Contract(_ContractID, FileName, getPhoto(), Now, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                frmContractEntry.getFile()
                Close()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមទាញយកឯកសារជាមុនសិន!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub insert_File_CasePlaintiff()
        Dim oFile As New cFile
        Dim FileName As String = System.IO.Path.GetFileName(_PhotoPath)
        If txtFile.Text <> "" Then
            If oFile.insert_File_CasePlaintiff(_CaseID, FileName, getPhoto(), Now, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                frmCaseEntry.getFileCasePlaintiff()
                Close()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមទាញយកឯកសារជាមុនសិន!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub insert_File_CaseDefendant()
        Dim oFile As New cFile
        Dim FileName As String = System.IO.Path.GetFileName(_PhotoPath)
        If txtFile.Text <> "" Then
            If oFile.insert_File_CaseDefendant(_CaseID, FileName, getPhoto(), Now, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                frmCaseEntry.getFileCaseDefendant()
                Close()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមទាញយកឯកសារជាមុនសិន!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub insert_File_CaseThirdParty()
        Dim oFile As New cFile
        Dim FileName As String = System.IO.Path.GetFileName(_PhotoPath)
        If txtFile.Text <> "" Then
            If oFile.insert_File_CaseThirdParty(_CaseID, FileName, getPhoto(), Now, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                frmCaseEntry.getFileCaseThirdParty()
                Close()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមទាញយកឯកសារជាមុនសិន!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class